<template lang="pug">
    skin(@up="up",@down="down",@yes="yes",@back="back",@duringUp="up",@duringDown="down")
        div(class="flex-column")
            div(class="flex-column-item flex-row h3")
                div(class="flex-row-item flex-2")
                    span 基础量:
                    span(class="active-block") {{model.amount}}U
                div(class="flex-row-item right")
                    span {{medicalCat.value}}
            div(class="flex-column-item flex-row h3")
                div(class="flex-row-item")
                    span 时差:{{timeDiff.value >=0 ? '+':''}}{{timeDiff.value}}h
                div(class="flex-row-item right")
                    span(class="line-2") {{medicalStrength.value}}
            div(class="flex-column-item flex-2 h3")
                rate-chart(v-model="rateValues")
        control-select(v-model="selectModel",:visible="selectVisible",ref="ask")
    
</template>

<script>
export default {
  data() {
    return {
      model: {
        amount: window.rate.basicRate.name,
        increment: 2,
        max: 40,
        min: 6
      },

      timeDiff: window.advance.timeDiff,
      medicalCat: window.advance.medicalCat,
      medicalStrength: window.advance.medicalStrength,

      selectVisible: false,
      selectModel: {
        name: "是否执行？",
        value: { name: "执行", value: true },
        options: [{ name: "执行", value: true }, { name: "退出", value: false }]
      },

        chart: null
    };
  },
    computed:{
        rateValues(){
            const amount = this.model.amount;
            let found =  window.fastRateTable.find(t=> {return t.name ==  amount});
            if(!found){
              found = window.fastRateTable[0];
              this.model.amount = found.name;
            }
              
            return found.value.map(t=> {return t.value});
              
        }
    },
  methods: {
    up() {
      if (this.selectVisible) {
        this.$refs.ask.up();
        return;
      }
      this.model.amount =
        this.model.amount == this.model.max
          ? this.model.min
          : (this.model.amount * 10 + this.model.increment * 10) / 10;
    },
    down() {
      if (this.selectVisible) {
        this.$refs.ask.down();
        return;
      }
      this.model.amount =
        this.model.amount == this.model.min
          ? this.model.max
          : (this.model.amount * 10 - this.model.increment * 10) / 10;
    },

    back() {
      if (!this.selectVisible) {
        if (this.model.amount == window.rate.basicRate.name)
          this.$router.push("/base-rate/index");
        else this.selectVisible = true;
      } else {
        if (this.selectModel.value.value == true) {
          const vm = this;
          window.rate.basicRate = window.fastRateTable.find(item => {
            return item.name == vm.model.amount;
          });
          // 写入到记录中
            window.rate.records.push(
                Object.assign(
                    {
                        type: window.advance.medicalCat.value,
                        createTime: new Date()
                    },
                    window.rate.basicRate
                )
            );
        }
        this.$router.push("/base-rate/index");
      }
    },
    yes() {
      if (!this.selectVisible) {
        if (this.model.amount == window.rate.basicRate.name)
          this.$router.push("/base-rate/index");
        else this.selectVisible = true;
        return;
      } else {
        if (this.selectModel.value.value == true) {
          const vm = this;
          window.rate.basicRate = window.fastRateTable.find(item => {
            return item.name == vm.model.amount;
          });
          // 写入到记录中
          window.rate.records.push(
            Object.assign(
              {
                type: window.advance.medicalCat.value,
                createTime: new Date()
              },
              window.rate.basicRate
            )
          );
        }
        this.$router.push("/base-rate/index");
      }
    },
  },
};
</script>